#include "bmp.h"

void load_bmp(const char *bmpfile, struct img_info *bmpinfo)
{
    FILE *fp = fopen(bmpfile, "r");

    struct bitmap_header head;
    struct bitmap_info   info;
    struct rgb_quad      quad;
    fread(&head, sizeof(head), 1, fp);
    fread(&info, sizeof(info), 1, fp);
    if(info.compression)
        fread(&quad, sizeof(quad), 1, fp);

    bmpinfo->width = info.width;
    bmpinfo->height= info.height;
    bmpinfo->bpp   = info.bit_count;
    printf("图片尺寸：%d × %d （色深%d比特）\n", bmpinfo->width,
                                                bmpinfo->height,
                                                bmpinfo->bpp);

    bmpinfo->size = info.width*info.height*info.bit_count/8;
    bmpinfo->rgb = calloc(1, bmpinfo->size);

    // 加载RGB数据
    int n, total=0, rgbsize=bmpinfo->size;
    while(rgbsize > 0)
    {
        n = fread(bmpinfo->rgb+total, 1, rgbsize, fp); // n就是实际读到的字节数

        rgbsize -= n;
        total   += n;
    }
    printf("total: %d\n", total);
    
    fclose(fp);
}



// void release_bmp(struct img_info *bmpinfo)
// {
//     // ..
// }
